package com.ruoyi.system.controller.person;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.AskLeave;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.Leave;
import com.ruoyi.system.dto.ChangeStatusDto;
import com.ruoyi.system.mapper.EmplyeeMapper;
import com.ruoyi.system.service.IEmplyeeService;
import com.ruoyi.system.service.ILeaveService;
import com.ruoyi.system.vo.LeaveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 离职管理 leaveController
 *
 * @author ruoyi
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/leave")
public class LeaveController extends BaseController
{
    @Autowired
    private ILeaveService leaveService;

    @Autowired
    private IEmplyeeService emplyeeService;

    /**
     * 查询leave列表
     */
    @RequiresPermissions("person:leave:list")
    @GetMapping("/list")
    public TableDataInfo list(Leave leave)
    {
        startPage();
        List<Leave> list = leaveService.selectLeaveList(leave);
        List<LeaveVo> listRes = new ArrayList<>();
        for (Leave leav : list) {
            LeaveVo leaveVo = new LeaveVo();
            Emplyee emplyee = emplyeeService.selectEmplyeeByEno(leav.getEno());
            leaveVo.setEmplyee(emplyee);
            BeanUtils.copyProperties(leav,leaveVo);
            listRes.add(leaveVo);
        }
        return getDataTable(listRes);
    }

    /**
     * 导出leave列表
     */
    @RequiresPermissions("person:leave:export")
    @Log(title = "leave", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Leave leave)
    {
        List<Leave> list = leaveService.selectLeaveList(leave);
        ExcelUtil<Leave> util = new ExcelUtil<Leave>(Leave.class);
        util.exportExcel(response, list, "leave数据");
    }

    /**
     * 获取leave详细信息
     */
    @RequiresPermissions("person:leave:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Leave leave = leaveService.selectLeaveById(id);
        LeaveVo leaveVo = new LeaveVo();
        leaveVo.setEmplyee(emplyeeService.selectEmplyeeByEno(leave.getEno()));
        BeanUtils.copyProperties(leave,leaveVo);
        return success(leaveVo);
    }

    /**
     * 新增leave
     */
    @RequiresPermissions("person:leave:add")
    @Log(title = "leave", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Leave leave)
    {
        return toAjax(leaveService.insertLeave(leave));
    }

    /**
     * 修改leave
     */
    @RequiresPermissions("person:leave:edit")
    @Log(title = "leave", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Leave leave)
    {
        System.out.println("修改leave："+leave);
        return toAjax(leaveService.updateLeave(leave));
    }

    /**
     * 修改审批状态
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ChangeStatusDto dto)
    {
        Leave leave = new Leave();
        leave.setId(dto.getId());
        leave.setStatus(dto.getStatus().toString());
        return toAjax(leaveService.updateLeave(leave));
    }

    /**
     * 删除leave
     */
    @RequiresPermissions("person:leave:remove")
    @Log(title = "leave", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveService.deleteLeaveByIds(ids));
    }
}
